<%@page import="com.wanmait.cookbook.vo.Pager"%>
<%@page import="com.wanmait.cookbook.vo.Cookbook"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%String path = request.getContextPath(); %>
<jsp:include page="/manage/inc/head.jsp"></jsp:include>
<jsp:include page="/manage/inc/menu.jsp"></jsp:include>
    <!--/sidebar-->
    <div class="main-wrap">

        <div class="crumb-wrap">
            <div class="crumb-list"><i class="icon-font"></i><a href="<%=path%>/manage/index.jsp">首页</a><span class="crumb-step">&gt;</span><span class="crumb-name">食谱管理</span></div>
        </div>
        <div class="search-wrap">
            <div class="search-content">
                <form action="<%=path %>/CookbookManageServlet?action=findCookbooksByInter" id="myform" method="post">
                    <table class="search-tab">
                    
                        <tr>
                        	<th width="120">选择分类:</th>
                            <td>
                                <select name="search-sort" id="chooseHot">
                                    <option value="18" >全部</option>
                                    <option value="19" >热门食谱</option>
                                    <option value="20" selected>非热门食谱</option>
                                </select>
                            </td>
                            <th width="70">关键字:</th>
                            <td><input class="common-text" placeholder="请输入食谱名"
							name="val" id="find" type="text"></td>
						<td><input class="btn btn-primary btn2" id="findbut" name="sub" value="查询"
							type="button"><span style="color:red" id="findspan"></span></td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
        <div class="result-wrap" >
            <form name="myform" id="myform1" method="post">
                <div class="result-title">
                    <div class="result-list">
                    
                        <a id="batchDel" href="javascript:void(0)"><i class="icon-font"></i>批量删除</a>
                        <a id="guanbi"  href="javascript:void(0)">关闭详情</a>
                    </div>
                </div>
                <div class="result-content">
                    <table class="result-tab" width="100%">
                        <tr>
                            <th class="tc" width="5%"><input class="allChoose" name="" type="checkbox"></th>
                            <th>序号</th>
                            <th>设置热门</th>
                            <th>食谱名称</th>
                            <th>分类</th>
                            <th>图片</th>
                            <th>上传者</th>
                            <th>浏览量</th>
                            <th>上传时间</th>
                            <th>收藏量</th>
                            <th>详情</th>
                            <th>操作</th>
                        </tr>
                        <%
                        	List<Cookbook> allNotHotCookbook =(List<Cookbook>)request.getAttribute("allNotHotCookbook");
                        	List<Integer> cookbookCollectCounts =(List<Integer>)request.getAttribute("nums");
                        	for(int i=0;i<allNotHotCookbook.size();i++) {	
                        %>
                        <tr  id="tr<%=allNotHotCookbook.get(i).getId()%>">
                            <td class="tc"><input data-id="<%=allNotHotCookbook.get(i).getId()%>" class="choose" name="id[]" type="checkbox"></td>
                            <td><%=i+1 %></td>
                            <td>
                                <select data-userinfoid="<%=allNotHotCookbook.get(i).getUserinfo().getId() %>" data-id="<%=allNotHotCookbook.get(i).getId()%>" class="sethot"  name="hot">
                                <%if(allNotHotCookbook.get(i).getHot()) {%>
                                	<option value="1">热门</option>
                                	<option value="0">不热门</option>
                                <%} else{%>
                                	<option value="0">不热门</option>
                                	<option value="1">热门</option>
                                <%} %>
                                </select>  
                            </td>
                            <td><%=allNotHotCookbook.get(i).getTitle() %></td>
                            <td><%=allNotHotCookbook.get(i).getRecipeTypeSecond().getTitle() %></td>
                            <td><img src="<%=path%>/static/image/<%=allNotHotCookbook.get(i).getPic() %>" width="50" height="50"></td>
                            <td><%=allNotHotCookbook.get(i).getUserinfo().getName() %></td>
                            <td><%=allNotHotCookbook.get(i).getPageviews() %></td>
                            <td><%=allNotHotCookbook.get(i).getInputTime() %></td>
                            <td><%=cookbookCollectCounts.get(i) %></td>
                            <td><a class="aaa" data-id="<%=allNotHotCookbook.get(i).getId() %>"
							src="javascript:void(0)">详情</a></td>

                            <td>
                                <a class="link-del"id="del<%=allNotHotCookbook.get(i).getId() %>" href="javascript:void(0)">删除</a>
                            </td>
                        </tr>
                        <!-- 详情弹窗 -->
						<style>
						.divv {
							width: 800;
							height: 800;
							display: none;
						}
						</style>
						<div id="ifr<%=allNotHotCookbook.get(i).getId() %>" style="float: left; width: 1000px; height: 600px" class="divv">
							<iframe id="myiframe" style="float: left;" src="<%=path %>/CookbookManageServlet?action=getCookbookItem&id=<%=allNotHotCookbook.get(i).getId() %>" width=1100px height=600px></iframe>
						</div>
						<!-- 详情弹窗结束 -->
                        <%
                        	}
                        %>
                      
                    </table>
                    <div class="list-page">
					<!-- 上一页 -->
					<a id="prev" href="javascript:void(0);">上一页</a>
							<!-- 显示数字 -->
							<%Pager pager = (Pager)request.getAttribute("pager"); %>
							<%
							int start = 1;
							int end = pager.getPageCount();
							if(pager.getPageCount()>5)
							{
								start = pager.getPageNo()-2<1?1:pager.getPageNo()-2;
								end = start+4;
								if(end>pager.getPageCount())
								{
									end = pager.getPageCount();
									start = end-4;
								}
							}
							for(int i=start;i<=end;i++)
							{
							%>
								<a class="number" href="javascript:void(0);"><%=i %></a>
							<% 
							}
							%>
							<!-- 显示数字的结束 -->
							<!-- 下一页 -->
					<a id="next" data-id="<%=pager.getPageNo() %>" href="javascript:void(0);">下一页</a>
					共<%=pager.getPageCount() %>页
				</div>
					<script src="<%=request.getContextPath()%>/manage/js/jquery.min.js"></script>
					<script type="text/javascript">
					$(function(){
						//下一页
						$("#next").click(function(){	 
							 	var n =<%=pager.getPageNo()%>
							 	if(n!=<%=pager.getPageCount()%>){
								 $.ajax({
									 url:"<%=request.getContextPath()%>/CookbookManageServlet?action=getManageNotHotCookbookListNextPager",
										type:"post",
										data:{pageNo:n+1},
										success:function(mes){
							
											$(".result-content").html(mes);
										}
									});//ajax的结束
							 	}
						});
					
					});
					</script>
                </div>
            </form>
        </div>
    </div>
    <!--/main-->
   <jsp:include page="/manage/inc/foot.jsp"></jsp:include>
<script>
$(function(){
	$("#chooseHot").change(function(){
		var val = $("#chooseHot").val();
		if(val=="18"){
			location.href="<%=path%>/CookbookManageServlet?action=getManageAllCookbookList";
		}else if(val=="19"){	
			location.href="<%=path%>/CookbookManageServlet?action=getManageHotCookbookList";
		}else if(val=="20"){	
			location.href="<%=path%>/CookbookManageServlet?action=getManageNotHotCookbookList";
		}
	});
		
	//设置热门
	$(".result-content").on("change",".sethot",function(){
	  var val = $(this).val();
	var id = $(this).data("id"); 
	var userinfoId = $(this).data("userinfoid");
	  $.ajax({
		url:"<%=path%>/CookbookManageServlet?action=setCookbookHot",
		data:{id:id,val:val,userinfoId:userinfoId},
		success:function(mes){
			
		}
	});  
});
 
//全选与反选
	$(".result-content").on("click",".allChoose",function(){
	//$(".allChoose").click(function(){
		$(".choose").prop("checked",$(".allChoose").prop("checked"));
	});
//单个删除
	$(".result-content").on("click",".link-del",function(){
	//$(".link-del").click(function() {
		var th = $(this);
		if(!window.confirm("确定要删除吗！")){
			//取消超级链接的默认行为
			//或者href="javascript:void(0)"bbb
			return;
		}
		var id = $(this).attr("id").substring(3);

		$.ajax({
			url:"<%=path%>/CookbookManageServlet?action=deleteCookbook",
			data:{id:id},
			success:function(){
				var pagerNo =$("#next").data("id");
				$.ajax({
					url:"<%=request.getContextPath()%>/CookbookManageServlet?action=getManageNotHotCookbookListNextPager",
					type:"post",
					data:{pageNo:pagerNo},
					success:function(mes){
		
						$(".result-content").html(mes);
					}
				});//ajax的结束
			}
		});
		
	});

	//删除全选按钮
	$("#batchDel").click(function(){
		 var  idArr = [];
		 $(".choose:checked").each(function(){
			 var id=$(this).data("id");
			 idArr.push(id);
		 })
		 if(!window.confirm("确定要删除吗！")){
				
				return;
		}
		 if(idArr.length>0){
			 $.get("<%=path %>/CookbookManageServlet?action=batchDelete",
					 {ids:idArr.toString()},
					 function(){
						 var pagerNo =$("#next").data("id");
						 
							$.ajax({
								url:"<%=request.getContextPath()%>/CookbookManageServlet?action=getManageNotHotCookbookListNextPager",
								type:"post",
								data:{pageNo:pagerNo},
								success:function(mes){
					
									$(".result-content").html(mes);
								}
							});//ajax的结束
				 })
		 }
		 
	});
	//点击详情打开隐藏iframe
	$(".result-content").on("click",".aaa",function(){
	//$(".aaa").click(function(){
		var id =$(this).data("id");
		$("#ifr"+id).css("display", "block");
		$("result-tab tr").css("display", "none");
	})
	//关闭详情
	$("#guanbi").click(function(){
		$("div[id^='ifr']").css("display", "none");
		$("result-tab tr").css("display", "block");
	});
	
	$(".result-content").on("click",".number",function(){
		//点击数字的时候执行
		//点击的数字变红
		$(".number").css("color","black");//所有数字变成黑色
		$(this).css("color","red");//点击的数字变成红色
		
		//获得点击的数字  $(this)是点击的a
		var n = $(this).html();
		//content是点击的a中间的内容  点击数字
		
		//从数据库中间查出新的一页的数据
		//Ajax
		$.ajax({
			url:"<%=request.getContextPath()%>/CookbookManageServlet?action=getManageNotHotCookbookListNextPager",
			type:"post",
			data:{pageNo:n},
			success:function(mes){
				$(".result-content").html(mes);
			}
		});//ajax的结束
		
	});//click事件的结束
	
	//搜索框不能为空
	$("#findbut").click(function(){
		var val =$("#find").val();
		if(val==""){
			$("#findspan").html("不能为空");
		}else{
			$("#findspan").html("");
			$("#myform").submit();
		}
	});
	
	
});
</script>